﻿/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is AST'r Framework.
  
  The Initial Developer of the Original Code is
  ALCARAZ Marc (aka eKameleon) <ekameleon@gmail.com>.
  Portions created by the Initial Developer are Copyright (C) 2004-2011
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
*/
package evideo.display 
{
    import evideo.logging.logger;
    import evideo.vo.VideoVO;

    import lunas.components.buttons.DashboardButton;
    import lunas.containers.ScrollContainer;

    import system.data.ValueObject;

    import vegas.models.maps.MapModelObject;

    import flash.display.DisplayObject;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.net.URLRequest;
    
    /**
     * The video list display.
     */
    public class VideoList extends ListValueObject 
    {
        /**
         * Creates a new VideoList instance.
         */
        public function VideoList()
        {
            //
        }
        
        /**
         * The left button reference.
         */
        public var leftButton:DisplayObject ;
        
        /**
         * The video model reference.
         */
        public var model:MapModelObject ;
        
        /**
         * The right button reference.
         */
        public var rightButton:DisplayObject ;
        
        /**
         * Clear the display.
         */
        public override function clear():void
        {
            super.clear() ;
            scroll() ;
        }
        
        /**
         * Run the process of the specified list.
         */
        public override function run( ...arguments:Array ):void
        {
            var vo:VideoVO = arguments[0] as VideoVO ;
            logger.info( this + " run " + vo ) ;
            if ( model != null && model.contains(vo) )
            {
                model.setCurrentVO( vo ) ;
            }
        }
        
        /**
         * Invoked when the container scroll change.
         */
        public function scroll( e:Event = null ):void 
        {
            var c:ScrollContainer = container as ScrollContainer ;
            // logger.info(this + " scroll : " + c.scroll + " / " + c.maxscroll + " :: " + leftButton + " :: " + rightButton ) ;
            if ( leftButton != null )
            {
                leftButton.visible = ( c != null ) && (c.maxscroll > 1) && (c.scroll > 1) ;
            }
            if ( rightButton != null )
            {
                rightButton.visible = ( c != null ) && (c.scroll !=  c.maxscroll) && (c.maxscroll > 1) ;
            }
        }
        
        /**
         * Sets a new cell in this container. 
         */
        protected override function setCell( cell:DisplayObject , vo:ValueObject ):DisplayObject
        {
            var video:VideoVO    = vo as VideoVO ;
            var bt:DashboardButton = super.setCell(cell, vo) as DashboardButton ;
            if ( bt != null && video != null )
            {
                bt.request = new URLRequest( video.thumb ) ;
                bt.label   = video.title || "" ;
                bt.addEventListener( MouseEvent.CLICK  , selectCell) ;
            }
            scroll() ;
            return bt ;
        } 
    }
}
